package com.en.controller.ZhSpeech;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.en.model.ZhSpeech;
import com.en.response.AppException;
import com.en.response.ErrorCodeMsgEnum;
import com.en.response.R;
import com.en.service.ZhSpeechService;
import com.en.utils.TTS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;


/**
 * @Description:
 * @Author: wht
 * @Date: 2024-01-21
 */
@Slf4j
@RestController
@RequestMapping("/")
@Api(tags = {"操作"})
public class ZhSpeechController {
    @Autowired
    ZhSpeechService zhSpeechService;

    @ApiOperation("新增一个")
    @PutMapping("/addOneZhSpeech")
    public <T> R<T> addOneZhSpeech(@Valid @RequestBody ZhSpeechAddDto addDto) {
        String speech_url = TTS.chineseToSpeech(addDto.getZh());
        addDto.setUrl(speech_url);

        ZhSpeech zhSpeech = new ZhSpeech();
        BeanUtils.copyProperties(addDto, zhSpeech);
        boolean res = zhSpeechService.save(zhSpeech);

        if (res) {
            return R.successMsg("添加成功！");
        } else {
            throw new AppException(ErrorCodeMsgEnum.ADD_ERROR);
        }
    }

    @ApiOperation("删除一个")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true)}
    )
    @DeleteMapping("/deleteOneZhSpeech/{id}")
    public <T> R<T> deleteOneZhSpeech(@PathVariable("id") Integer id) {
        if (id == null) {
            throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
        boolean res = zhSpeechService.removeById(id);

        if (res) {
            return R.successMsg("删除成功！");
        } else {
            throw new AppException(ErrorCodeMsgEnum.DELETE_ERROR);
        }
    }

    @ApiOperation("修改一个信息")
    @PatchMapping("/updateOneZhSpeech")
    public <T> R<T> updateOneZhSpeech(@RequestBody ZhSpeechUpdateDto updateDto) {
        ZhSpeech zhSpeech = new ZhSpeech();
        BeanUtils.copyProperties(updateDto, zhSpeech);
        boolean res = zhSpeechService.updateById(zhSpeech);

        if (res) {
            return R.successMsg("修改成功！");
        } else {
            throw new AppException(ErrorCodeMsgEnum.UPDATE_ERROR);
        }
    }

    @ApiOperation("通过id获取一个信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true)}
    )
    @GetMapping("/getOneZhSpeech/{id}")
    public <T> R<T> getOneZhSpeech(@PathVariable("id") Integer id) {
        if (id == null) {
            throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
        ZhSpeech zhSpeech = zhSpeechService.getById(id);
        return R.successData(zhSpeech);
    }

    @ApiOperation("分页获取列表")
    @PostMapping("/getZhSpeechList")
    public <T> R<T> getZhSpeechList(@Valid @RequestBody ZhSpeechSearchDto searchDto) throws IllegalAccessException {
        QueryWrapper<ZhSpeech> queryWrapper = new QueryWrapper<>();

        Field[] fields = searchDto.getClass().getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            if (name.equals("page_num") || name.equals("page_size")) continue;
            field.setAccessible(true); // 必须将字段设置为可访问
            Object value = field.get(searchDto);
            if (value != null) {
                queryWrapper.eq(name, value);
            }
        }

        Page<ZhSpeech> page = new Page<>(searchDto.getPage_num(), searchDto.getPage_size());
        zhSpeechService.page(page, queryWrapper);

        HashMap<Object, Object> res = new HashMap<>();
        res.put("page_size", page.getSize());
        res.put("page_num", page.getCurrent());
        res.put("total", page.getTotal());
        res.put("list", page.getRecords());
        return R.successData(res);
    }

    @ApiOperation("获取所有列表")
    @GetMapping("/getAllZhSpeechList")
    public <T> R<T> getAllZhSpeechList() {
        List<ZhSpeech> list = zhSpeechService.list();

        return R.successData(list);
    }
}
